﻿package Welsy4.Connect.commands {
	import Welsy4.Connect.business.CommonInfoDelegate;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.events.EventChainFactory;
	import Welsy4.Connect.events.MeetingEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.Meeting;

	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;

	import flash.net.URLVariables;

	import mx.rpc.IResponder;
	import mx.controls.Alert;


	public class DeleteMeetingIfExistsCommand extends SequenceCommand implements Command, IResponder {

		private var eventURLVariable : URLVariables;

		override public function execute(event : CairngormEvent) : void {

			ConnectModel.getInstance().addQueue("DeleteMeetingIfExistsCommand.execute: " + ConnectModel.getInstance().SCOShortcutRequested.sco_id);

			this.nextEvent = ChainEvent(event).nextChainedEvent;
			this.eventURLVariable = ChainEvent(event).eventRequest;

			var SCOshortcutDelegate : CommonInfoDelegate = new CommonInfoDelegate(this);

			// SCOShortcutRequested.sco_id è l'id della meeting folder
			SCOshortcutDelegate.getSCOContents(ConnectModel.getInstance().SCOShortcutRequested.sco_id, "meeting", eventURLVariable.name);
		}

		public function result(data : Object) : void {

			ConnectModel.getInstance().addQueue("DeleteMeetingIfExistsCommand.result");

			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(data.result);

			if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
				ConnectModel.getInstance().lastMeetingFound = new Meeting;

				if (ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.length() > 0) {
					
					ConnectModel.getInstance().addQueue("DeleteMeetingIfExistsCommand.result: SCO found");
					
					ConnectModel.getInstance().lastMeetingFound.sco_id = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.attribute("sco-id");
					ConnectModel.getInstance().lastMeetingFound.name = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.name;



					var chainEvent:ChainEvent = EventChainFactory.chainEvents([new MeetingEvent(MeetingEvent.DELETE_MEETING),this.nextEvent]);
					
					CairngormEventDispatcher.getInstance().dispatchEvent(chainEvent);

				} else {
					ConnectModel.getInstance().addQueue("DeleteMeetingIfExistsCommand.result: SCO NOT found");
					
					this.executeNextCommand();
				}
			}
			else {

				CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.GENERIC, data.result));
			}




		}

		public function fault(data : Object) : void {
			ConnectModel.getInstance().addQueue("GetMeetingSCOidCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}

	}

}